import type { GithubPageType } from '../../../../../../lib/page_handler/platform/github/types'
import type { AutoPagination } from '../../index'
import { BaiduPageType } from '../../../../../../lib/page_handler/platform/baidu/types'
import { BIAN_PAGINATION_CONFIG } from '../../../../../../lib/page_handler/platform/bian/types'
import { GITEE_PAGINATION_CONFIG } from '../../../../../../lib/page_handler/platform/gitee/types'
import { GITHUB_PAGE_PAGINATION_CONFIG } from '../../../../../../lib/page_handler/platform/github/types'
import { GREASY_FORK_PAGINATION_CONFIG } from '../../../../../../lib/page_handler/platform/greasyfork/types'
import { HOSTNAME, SUPPORTED_PLATFORMS } from '../../../../../../types/constants'
import { IPaginationLoadType } from '../../index'

export class AutoPaginationConfig {
  private static instance: AutoPaginationConfig

  constructor(private paginationInstance: AutoPagination) {
    this.paginationInstance = paginationInstance
  }

  /**
   * 获取单例实例
   */
  public static getInstance(paginationInstance: AutoPagination): AutoPaginationConfig {
    if (!AutoPaginationConfig.instance) {
      AutoPaginationConfig.instance = new AutoPaginationConfig(paginationInstance)
    }
    return AutoPaginationConfig.instance
  }

  /**
   * 获取分页加载类型
   */
  getPageLoadType(): IPaginationLoadType {
    switch (HOSTNAME) {
      case SUPPORTED_PLATFORMS.BAI_DU:
        return this.paginationInstance.baiduService.loadType
      case SUPPORTED_PLATFORMS.GREASY_FORK:
        return this.paginationInstance.greasyForkService.loadType
      case (SUPPORTED_PLATFORMS.BIAN):
        return this.paginationInstance.bianService.loadType
      case SUPPORTED_PLATFORMS.GITEE:
        return this.paginationInstance.giteeService.loadType
      case SUPPORTED_PLATFORMS.GITHUB:
        return this.paginationInstance.githubService.loadType
      default:
        return IPaginationLoadType.Pagination
    }
  }

  /**
   * 获取选择器
   */
  getPageSelector(): string {
    switch (HOSTNAME) {
      case SUPPORTED_PLATFORMS.BAI_DU: // 搜索框选择器
        // 搜索框选择器
        switch (this.paginationInstance.pageType) {
          case BaiduPageType.Web:
            return '#chat-textarea'
          case BaiduPageType.News:
            return '#kw'
          default:
            return ''
        }
      case SUPPORTED_PLATFORMS.GREASY_FORK: // 【分页组件】下一页按钮选择器
        return GREASY_FORK_PAGINATION_CONFIG.selector.link
      case (SUPPORTED_PLATFORMS.BIAN):
        return BIAN_PAGINATION_CONFIG.selector.link
      case SUPPORTED_PLATFORMS.GITEE:
        return GITEE_PAGINATION_CONFIG.selector.link
      case SUPPORTED_PLATFORMS.GITHUB:
        return GITHUB_PAGE_PAGINATION_CONFIG[this.paginationInstance.pageType as GithubPageType]?.selector.nextPage || ''
      default:
        return ''
    }
  }

  /**
   * 获取分页组件页码选择器
   */
  getPageNumSelector(): string {
    switch (HOSTNAME) {
      case (SUPPORTED_PLATFORMS.BAI_DU): {
        switch (this.paginationInstance.pageType) {
          case BaiduPageType.Web:
            return '#page div strong'
          case BaiduPageType.News:
            return '#page .page-inner strong'
          default:
            return ''
        }
      }
      case (SUPPORTED_PLATFORMS.GREASY_FORK):
        return GREASY_FORK_PAGINATION_CONFIG.selector.currentPage
      case (SUPPORTED_PLATFORMS.BIAN):
        return BIAN_PAGINATION_CONFIG.selector.currentPage
      case SUPPORTED_PLATFORMS.GITEE:
        return GITEE_PAGINATION_CONFIG.selector.currentPage
      default:
        return ''
    }
  }

  /**
   * 获取页面布局修正样式
   */
  getPageLayoutFixStyle(): string {
    switch (HOSTNAME) {
      case (SUPPORTED_PLATFORMS.BAI_DU):
        return `
          width: calc(100% - 15px) !important;
          min-width: auto !important;
        `
      default:
        return ''
    }
  }
}
